package com.zhuiYun.project.util;

import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *
 *  利用redis生成数据库全局唯一性id
 * @ClassName DD
 * @Description TODO
 * @Author zcy
 * @Date 2023/3/15 14:36
 **/
@Component
public class PrimaryKeyUtil {



    /**
     * 生成订单号
     * @return
     */
    public String getOrderId() {
        String prefix = DateTimeUtils.format("yyyyMMddHHmm", new Date());
        String key = "ORDER_ID_" + prefix;
        String orderId = null;
        try {
            Long increment = RedisUtil.incr(key,1);
            //往前补6位
            orderId=prefix+String.format("%1$06d",increment);
        } catch (Exception e) {
            System.out.println("生成单号失败");
            e.printStackTrace();
        }
        return orderId;
    }

}
